from django.conf import settings
from rest_framework import serializers
from api.models import Teachers


class TeacherSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField()
    gender = serializers.SerializerMethodField()

    def get_gender(self, obj):
        return obj.get_gender_display()

    pic = serializers.SerializerMethodField()

    def get_pic(self, obj):
        return '%s%s%s' % ('http://127.0.0.1:8000/', settings.MEDIA_URL, str(obj.pic))


class TeacherDeSerializer(serializers.Serializer):
    username = serializers.CharField(
        max_length=3,
        min_length=2,
        error_messages= {
            "max_length": "名字太长了",
            "min_length": "名字太短了",
        }
    )
    password = serializers.CharField()
    phone = serializers.CharField

    def create(self, validated_data):
        return Teachers.objects.create(**validated_data)
